package com.ruoyi.exam.domain;

import com.ruoyi.common.exception.ItemNotFoundException;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.exam.enums.ExamStatusEnum;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
public class ExamInfo {
    private Integer id; // 考试ID
    private String examName;// 考试名称
    private Integer status; // 考试状态0未进行 1进行中 2考试结束 -1未启用
    private Integer startTime;  //开始时间
    private Integer endTime;    //结束时间

    private Date createTime;
    private Date updateTime;

    private ExamTheory examTheory; //理论考试


    /**
     * 检查考试信息是否完备
     */
    public void verifyComplete(){
        ObjectUtil.checkNull(examName ,"examName");
        ExamStatusEnum statusEnum = ExamStatusEnum.parse(status);

        if(startTime >= endTime){
            throw new IllegalArgumentException("startTime["+ startTime +"] >= endTime["+ endTime +"]");
        }

        long now = System.currentTimeMillis() / 1000;
        boolean notMatchStatus = false;
        if(statusEnum == ExamStatusEnum.CREATE_NOT_START){
            if(startTime < now){
                notMatchStatus =true;
            }
        }else if(statusEnum == ExamStatusEnum.IN_PROGRESS){
            if(startTime >= now || endTime < now){
                notMatchStatus =true;
            }
        }else if(statusEnum == ExamStatusEnum.ENDED){
            if(endTime <= now){
                notMatchStatus =true;
            }
        }
        if(notMatchStatus){
            throw new IllegalArgumentException("startTime["+ startTime +"] or endTime["+ endTime +"] not match status[" + status + "]");
        }

        ObjectUtil.checkNull(examTheory ,"examTheory");
        ObjectUtil.checkNull(examTheory.getPaperId() ,"exam paperId");

        if(examTheory.getExamTime() == null || examTheory.getExamTime() <= 0){
            throw new IllegalArgumentException("examTime was empty");
        }
    }

    /**
     * 设置默认状态
     */
    public void setDefaultStatus(){
        status = ExamStatusEnum.CREATE_NOT_START.getStatus();
        long now = System.currentTimeMillis() / 1000;
        if(startTime >= now){
            status = ExamStatusEnum.IN_PROGRESS.getStatus();
        }
        if(endTime <= now){
            status = ExamStatusEnum.ENDED.getStatus();
        }
    }

    /**
     * 检查状态是否需要更新，需要时则更新状态
     * @return 状态是否更新
     */
    public boolean checkAndSetStatus(){
        ExamStatusEnum statusEnum = ExamStatusEnum.parse(status);
        if(statusEnum == ExamStatusEnum.CREATE_NOT_START || statusEnum == ExamStatusEnum.IN_PROGRESS){
            long now = System.currentTimeMillis() / 1000;
            if(startTime >= now){
                status = ExamStatusEnum.IN_PROGRESS.getStatus();
            }
            if(endTime < now){
                status = ExamStatusEnum.ENDED.getStatus();
            }
        }
        return false;
    }

    public void setDataTime(){
        createTime = new Date();
        updateTime = createTime;
    }

}
